﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admins_Order : System.Web.UI.Page
{
     public static int Id;
    protected static int tong_tien;
    protected void Page_Load(object sender, EventArgs e)
    {
        grid_order.ItemCommand += new DataGridCommandEventHandler(grid_order_ItemCommand);
        grid_contach_order.ItemDataBound += new DataGridItemEventHandler(grid_contach_order_ItemDataBound);
        grid_order.PageIndexChanged += new DataGridPageChangedEventHandler(grid_order_PageIndexChanged);
        if (!IsPostBack)
        {
            layOrder();
        }
    }

    public void layOrder()
    {
        Order order = new Order();
        List<ObjOrder> lstOrder = new List<ObjOrder>();
        lstOrder = order.getOrderByStatus(Constants.STATUS_ALL);
        if (lstOrder.Count > 0)
        {
            grid_order.DataSource = lstOrder;
            grid_order.DataBind();
        }
        else
        {
            lb_messenger.Text = "Chưa có giỏ hàng";
        }
        pnView.Visible = true;
        pnUpdate.Visible = false;
    }
    void grid_order_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grid_order.CurrentPageIndex = e.NewPageIndex;
        layOrder();
    }

    void grid_contach_order_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            int gia =int.Parse( DataBinder.Eval(e.Item.DataItem,"gia").ToString());
            int so_luong = int.Parse(DataBinder.Eval(e.Item.DataItem, "so_luong").ToString());
            tong_tien += gia * so_luong;

        }
    }

    void grid_order_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string strCA = e.CommandArgument.ToString();
        Id = int.Parse(strCA);
        switch (e.CommandName)
        {
            case "Edit":
                pnUpdate.Visible = true;
                pnView.Visible = false;
                ContachOrder contactOrder = new ContachOrder();
                List<ObjContactOrder> lst = new List<ObjContactOrder>();
                lst = contactOrder.getContactOrder(Id);
                if (lst.Count > 0)
                {
                    grid_contach_order.DataSource = lst;
                    grid_contach_order.DataBind();
                }
                ck_giao_hang.Checked = true;
                if (e.Item.Cells[2].Text == "1")
                {
                    ck_giao_hang.Checked = false;
                }
                break;

            case "Delete":
                   Order order = new Order();
                order.updateOrderGiaoHang(0, Id);
                Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                break;

        }
    }
    protected void AddButton_Click(object sender, EventArgs e)
    {
      

    }
    protected void RefreshButton_Click(object sender, EventArgs e)
    {
        layOrder(); 
    }
    protected void Update_Click(object sender, EventArgs e)
    {
        if(Id!=0)
        {
            Order order = new Order();
            int status = 1;
            if (ck_giao_hang.Checked == true)
            {
                status = 2;
            }
            order.updateOrderGiaoHang(status, Id);
            Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }
    }
    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        string xml = "";
        
        foreach (DataGridItem item in grid_order.Items)
        {
            CheckBox checkBox = (CheckBox)item.Cells[0].Controls[1];

            if (checkBox.Checked == true)
            {
                xml += "<Order Order_id=\"" + item.Cells[1].Text + "\" ></Order>";
            }
        }
        if (xml != "")
        {
            Order order = new Order();
            order.deleteOrderByXml(xml);
            Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }
    }

    protected void btn_tim_kiem_Click(object sender, EventArgs e)
    {
        Order order = new Order();
        List<ObjOrder> lst = order.getOrderSearch(txt_tim_kiem.Text);
        if (lst.Count > 0)
        {
            grid_order.DataSource = lst;
            grid_order.DataBind();
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        pnView.Visible = true;
        pnUpdate.Visible = false;
    }
}